import { S, U, getRequestId, mapField } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import env from '../../config/common'

export default {
  // 获取sms
  getSmsInfo ({ dispatch, commit, state, rootState }) {
    return Q.Promise((success, error) => {
      axios.get(
        env.SMS_API_HOST + '/sites/' + rootState.auth.site_id + '/sms_info'
      ).then((response) => {
        if (response.data.results) {
          const smsInfo = response.data.results
          response.data.results.data = smsInfo
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  // 保存sms
  saveSmsInfo ({ dispatch, commit, state, rootState }, info) {
    const params = S({
      info: {
        is_open_sms: info.is_open_sms,
        sms_sign: info.sms_sign
      },
      trace: {
        requestId: getRequestId()
      }
    })
    return Q.Promise((success, error) => {
      axios.post(
        env.SMS_API_HOST + '/sites/' + rootState.auth.site_id + '/sms_info',
        params,
        { headers: {
          'Content-Type': 'application/json;charset=UTF-8'
        }}
      ).then((response) => {
        if (response.data.results) {
          const smsInfo = response.data.results
          response.data.results.data = smsInfo
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  }
}
